// CrossWorks Tasking Library.
//
// Copyright (c) 2005 Rowley Associates Limited.
//
// This file may be distributed under the terms of the License Agreement
// provided with this software.
//
// THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

#include <ctl_api.h>

#define VICIRQStatus (*(volatile unsigned long *)0xFFFFF000)
#define VICFIQStatus (*(volatile unsigned long *)0xFFFFF004)
#define VICRawIntr (*(volatile unsigned long *)0xFFFFF008)
#define VICIntSelect (*(volatile unsigned long *)0xFFFFF00C)
#define VICIntEnable (*(volatile unsigned long *)0xFFFFF010)
#define VICIntEnClr (*(volatile unsigned long *)0xFFFFF014)
#define VICSoftInt (*(volatile unsigned long *)0xFFFFF018)
#define VICSoftIntClear (*(volatile unsigned long *)0xFFFFF01C)
#define VICProtection (*(volatile unsigned long *)0xFFFFF020)
#define VICSWPriorityMask (*(volatile unsigned long *)0xFFFFF024)
#define VICVectAddr0 (*(volatile unsigned long *)0xFFFFF100)
#define VICVectAddr1 (*(volatile unsigned long *)0xFFFFF104)
#define VICVectAddr2 (*(volatile unsigned long *)0xFFFFF108)
#define VICVectAddr3 (*(volatile unsigned long *)0xFFFFF10C)
#define VICVectAddr4 (*(volatile unsigned long *)0xFFFFF110)
#define VICVectAddr5 (*(volatile unsigned long *)0xFFFFF114)
#define VICVectAddr6 (*(volatile unsigned long *)0xFFFFF118)
#define VICVectAddr7 (*(volatile unsigned long *)0xFFFFF11C)
#define VICVectAddr8 (*(volatile unsigned long *)0xFFFFF120)
#define VICVectAddr9 (*(volatile unsigned long *)0xFFFFF124)
#define VICVectAddr10 (*(volatile unsigned long *)0xFFFFF128)
#define VICVectAddr11 (*(volatile unsigned long *)0xFFFFF12C)
#define VICVectAddr12 (*(volatile unsigned long *)0xFFFFF130)
#define VICVectAddr13 (*(volatile unsigned long *)0xFFFFF134)
#define VICVectAddr14 (*(volatile unsigned long *)0xFFFFF138)
#define VICVectAddr15 (*(volatile unsigned long *)0xFFFFF13C)
#define VICVectAddr16 (*(volatile unsigned long *)0xFFFFF140)
#define VICVectAddr17 (*(volatile unsigned long *)0xFFFFF144)
#define VICVectAddr18 (*(volatile unsigned long *)0xFFFFF148)
#define VICVectAddr19 (*(volatile unsigned long *)0xFFFFF14C)
#define VICVectAddr20 (*(volatile unsigned long *)0xFFFFF150)
#define VICVectAddr21 (*(volatile unsigned long *)0xFFFFF154)
#define VICVectAddr22 (*(volatile unsigned long *)0xFFFFF158)
#define VICVectAddr23 (*(volatile unsigned long *)0xFFFFF15C)
#define VICVectAddr24 (*(volatile unsigned long *)0xFFFFF160)
#define VICVectAddr25 (*(volatile unsigned long *)0xFFFFF164)
#define VICVectAddr26 (*(volatile unsigned long *)0xFFFFF168)
#define VICVectAddr27 (*(volatile unsigned long *)0xFFFFF16C)
#define VICVectAddr28 (*(volatile unsigned long *)0xFFFFF170)
#define VICVectAddr29 (*(volatile unsigned long *)0xFFFFF174)
#define VICVectAddr30 (*(volatile unsigned long *)0xFFFFF178)
#define VICVectAddr31 (*(volatile unsigned long *)0xFFFFF17C)
#define VICVectPriority0 (*(volatile unsigned long *)0xFFFFF200)
#define VICVectPriority1 (*(volatile unsigned long *)0xFFFFF204)
#define VICVectPriority2 (*(volatile unsigned long *)0xFFFFF208)
#define VICVectPriority3 (*(volatile unsigned long *)0xFFFFF20C)
#define VICVectPriority4 (*(volatile unsigned long *)0xFFFFF210)
#define VICVectPriority5 (*(volatile unsigned long *)0xFFFFF214)
#define VICVectPriority6 (*(volatile unsigned long *)0xFFFFF218)
#define VICVectPriority7 (*(volatile unsigned long *)0xFFFFF21C)
#define VICVectPriority8 (*(volatile unsigned long *)0xFFFFF220)
#define VICVectPriority9 (*(volatile unsigned long *)0xFFFFF224)
#define VICVectPriority10 (*(volatile unsigned long *)0xFFFFF228)
#define VICVectPriority11 (*(volatile unsigned long *)0xFFFFF22C)
#define VICVectPriority12 (*(volatile unsigned long *)0xFFFFF230)
#define VICVectPriority13 (*(volatile unsigned long *)0xFFFFF234)
#define VICVectPriority14 (*(volatile unsigned long *)0xFFFFF238)
#define VICVectPriority15 (*(volatile unsigned long *)0xFFFFF23C)
#define VICVectPriority16 (*(volatile unsigned long *)0xFFFFF240)
#define VICVectPriority17 (*(volatile unsigned long *)0xFFFFF244)
#define VICVectPriority18 (*(volatile unsigned long *)0xFFFFF248)
#define VICVectPriority19 (*(volatile unsigned long *)0xFFFFF24C)
#define VICVectPriority20 (*(volatile unsigned long *)0xFFFFF250)
#define VICVectPriority21 (*(volatile unsigned long *)0xFFFFF254)
#define VICVectPriority22 (*(volatile unsigned long *)0xFFFFF258)
#define VICVectPriority23 (*(volatile unsigned long *)0xFFFFF25C)
#define VICVectPriority24 (*(volatile unsigned long *)0xFFFFF260)
#define VICVectPriority25 (*(volatile unsigned long *)0xFFFFF264)
#define VICVectPriority26 (*(volatile unsigned long *)0xFFFFF268)
#define VICVectPriority27 (*(volatile unsigned long *)0xFFFFF26C)
#define VICVectPriority28 (*(volatile unsigned long *)0xFFFFF270)
#define VICVectPriority29 (*(volatile unsigned long *)0xFFFFF274)
#define VICVectPriority30 (*(volatile unsigned long *)0xFFFFF278)
#define VICVectPriority31 (*(volatile unsigned long *)0xFFFFF27C)
#define VICAddress (*(volatile unsigned long *)0xFFFFFF00)
#define VICVectAddrn(n) (*(&VICVectAddr0 + n))
#define VICVectPriority(n) (*(&VICVectPriority0 + n))

int
ctl_set_isr(unsigned int number, unsigned int priority, CTL_ISR_TRIGGER_t trigger, CTL_ISR_FN_t isr, CTL_ISR_FN_t *oldisr)
{
  if (oldisr)
    *oldisr = (CTL_ISR_FN_t)VICVectAddrn(number);
  if (isr)
    {
      /* Installing an ISR */
#ifdef CHECK_INTERRUPT_INSTALLED
      if (VICVectPriority(number))
        return 0; /* ISR already installed at this interrupt priority */
#endif
      VICIntSelect &= ~(1 << number); /* IRQ Interrupt */
      VICVectPriority(number) = priority;
      VICVectAddrn(number) = (unsigned int)isr;
    }
  else
    {
      /* Removing an ISR */
      VICVectPriority(priority) = 0;
      VICVectAddrn(priority) = 0xF;
    }
  return 1;
}

int
ctl_unmask_isr(unsigned int number)
{
  VICIntEnable = 1 << number;
  return 1;
}

int
ctl_mask_isr(unsigned int number)
{
  VICIntEnClr = 1 << number;
  return 1;
}
